package com.yjd.bi.report.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.bi.base.service.impl.BaseServiceImpl;
import com.yjd.bi.report.model.BiTableInfoModel;
import com.yjd.bi.report.service.IBiTableInfoService;
import com.yjd.bi.report.dao.IBiTableInfoDao;

/**
 * <b>description</b>：表信息业务实现<br>
 * <b>time</b>：2016-04-08 15:29:20 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Service("biTableInfoService")
public class BiTableInfoServiceImpl extends BaseServiceImpl implements
		IBiTableInfoService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public BiTableInfoModel insert(BiTableInfoModel model) throws Exception {
		this.disposeBeforeSave(model);
		return this.biTableInfoDao.insertModel(model, true);
	}

	public void disposeBeforeSave(BiTableInfoModel model) {
		if (model == null || model.getDatabase_info_id() == null
				|| StringUtil.isEmpty(model.getTable_name())
				|| StringUtil.isEmpty(model.getDescription())) {
			FrameUtil.throwBaseException1("所属数据源、表名、主键、备注不能为空");
		}
		model.setTable_name(model.getTable_name().toLowerCase());
		model.setPk_field_name(model.getPk_field_name().toLowerCase());
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(BiTableInfoModel model) throws Exception {
		this.disposeBeforeSave(model);
		return this.biTableInfoDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.biTableInfoDao.deleteModel(paramMap, true);
	}

	@Resource
	private IBiTableInfoDao biTableInfoDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.biTableInfoDao;
	}

}
